/*----------
 *ajax数据请求
 *补助页面
 */

var table = new Vue({
    el: '#main-data',
    data: {
        items: [],
        new_item: [],
        message: 'message',
        query: {
            "rows": 10,
            "page": 1
        },
        add_item: '',
        temp: '',
        url: {
            'queryZG': 'http://help.ngrok.cc/studentmanagement/base/stu/list.do',
            'addBuzhu': 'http://help.ngrok.cc/studentmanagement/usr/buzhu/add.do',
            'submit': 'http://help.ngrok.cc/studentmanagement/usr/buzhu/submit.do',
            'delBuzhu': 'http://help.ngrok.cc/studentmanagement/usr/buzhu/remove.do'
        }
    },
    methods: {
        /*显示数据*/
        showData: function() {
            var _self = this;
            $.getJSON("../json/data.json", function(result) {
                _self.items = result;
            });
            console.log(_self.message);
        },
        /*添加数据到下方的表中，不提交*/
        join: function() {
            var _self = this;
            _self.add_item.helpDate = _self.temp.helpDate;
            _self.add_item.money = _self.temp.money;

            var data = {
                "helpDate": _self.add_item.helpDate,
                "deptId": _self.add_item.deptId,
                "stuId": _self.add_item.stuId,
                "szlbdm": 10,
                "shztm": 1,
                "remark": "buzhu",
                "money": parseInt(_self.add_item.money)
            }
            $.ajax({
                type: "POST",
                url: _self.url.addBuzhu,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                dataType: "json",
                success: function(result) {
                    if (result.status === 1) {
                        _self.items.push(_self.add_item);
                        $('#myModal').modal('hide');
                    } else {
                        alert(result.message);
                    }
                    console.log(result);
                },
                error: function(message) {
                    alert("提交数据失败！");
                }
            });
        },
        /*用户提交查询信息，查询学生信息*/
        queryData: function() {
            var _self = this;
            /*提交数据到服务器 query是要查询的数据对象*/
            console.log(_self.query);

            $.ajax({
                type: "POST",
                url: _self.url.queryZG,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(_self.query),
                dataType: "json",
                success: function(result) {
                    console.log(result);
                    $.each(result.result, function(i, val) {
                        _self.new_item.push(val)
                    });
                },
                error: function(message) {
                    alert("提交数据失败！");
                }
            });
        },
        setItem: function(item) {
            console.log(item);
            var _self = this;
            _self.add_item = item;
        },
        submitData: function() {
            var _self = this;
            var id = '';
            var len = _self.items.length;
            $.each(_self.items, function(i, val) {
                if (i === len - 1) {
                    id = id + val.id;
                } else {
                    id = id + val.id + ',';
                }
            });
            var data = {
                "buzhuList": id
            }
            console.log(data);
            var newUrl = _self.url.submit + '?' + 'buzhuList=' + id;
            console.log(newUrl);
            $.ajax({
                type: "POST",
                url: newUrl,
                contentType: "application/json; charset=utf-8",
                // data: ,
                dataType: "json",
                success: function(result) {
                    alert(result.message);
                },
                error: function(message) {
                    alert("提交数据失败！");
                }
            });
        }
    }
})
